var express = require('express');
var router = express.Router();

var http = require('http');
var url=require('url');

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// 轮播图路由
router.get('/lunbo', function (req, res) {
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/billboard/home?__t=' + t;
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 正在热映路由
router.get('/hoting', function (req, res) {
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/film/now-playing?__t='+t+'&page=1&count=5';
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 即将上映路由
router.get('/willhot', function (req, res) {
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__t='+t+'&page=1&count=3';
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 影片详情路由
router.get(/\d$/, function (req, res) {
	var url = 'http://m.maizuo.com/v4/api/film'+req.url;
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 正在热映影片路由
router.get('/now', function (req, res) {
	var num=req.query.num;
	var url = 'http://m.maizuo.com/v4/api/film/now-playing?page='+num+'&count=7';
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 即将上映影片路由
router.get('/soon', function (req, res) {
	var num=req.query.num;
	var url = 'http://m.maizuo.com/v4/api/film/coming-soon?page='+num+'&count=7';
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

// 全部影院路由
router.get('/cinema', function (req, res) {
	var t=req.query.time;
	var url = 'http://m.maizuo.com/v4/api/cinema?__t='+t;
	http.get(url, function (response) {
		var data = '';
		response.on('data', function (thunk) {
			data += thunk;
		});
		response.on('end', function () {
			res.send(data);
		})
	})
})

module.exports = router;
